


#include "mem_ctrl.h"


/**
 * @brief 
 * @param data_list 目标数据序列首地址
 * @param move_n    向左移位 move_n 个
 * @param _length   目标序列的长度
 */
void move_uint32_func(uint32_t *data_list,uint16_t move_n,uint16_t _length)
{
    uint16_t i;
    for(i=0;i<_length-move_n;i++)
    {
        data_list[i] = data_list[move_n+i];
    }
}



void move_int32 ( int32_t *data_list,uint16_t move_n,uint16_t _length)
{
    uint16_t i;
    for(i=0;i<_length-move_n;i++)
    {
        data_list[i] = data_list[move_n+i];
    }
}

/*!
 @brief 构造一个数据 列表
 
 @param data_block :指定的数据内存块，一个数组
 @param data_size  ：数据块的大小
 @param _list      ： 列表句柄
 @return uint8_t 
 */

uint8_t init_list(uint32_t *data_block,uint16_t data_size,mem_control *_list)
{
      _list-> data_ptr = data_block;
      _list-> __max_data_len = data_size;
      _list-> data_len = 0;
      return 1;
}


/*!
 @brief 向 tar_list 增加一个数据
 @param one_data  
 @param tar_list 
 @return uint8_t 
 */
uint8_t append_data(uint32_t one_data , mem_control *tar_list)
{
  // 数 据 未 满 
   if( tar_list->data_len < tar_list->__max_data_len)
   {
    // move_uint32(tar_list->data_ptr,1,tar_list->data_len);
    tar_list->data_ptr[tar_list->data_len] = one_data;
    tar_list->data_len++;
    return 1;
   }
   return 0;
}
/*!
 @brief 删除左边的第一个数据
 @param tar_list 
 @return uint8_t 
 */
uint8_t pop_left(mem_control *tar_list)
{
    if(tar_list->data_len == 0)
    {
        return 0;
    }
   // 数据 队列  未满 或 满
   if( tar_list->data_len <= tar_list->__max_data_len)
   {
    move_uint32_func(tar_list->data_ptr,1,tar_list->data_len);
    tar_list->data_ptr[tar_list->data_len-1] = 0;
    tar_list->data_len = tar_list->data_len -1 ;
   }
    return 1;
}
/*!
 @brief 清空队列
 
 @param tar_list 
 @return uint8_t 
 */
uint8_t clear_list(mem_control *tar_list)
{
    uint16_t idex = 0;
    for(idex=0;idex<tar_list->__max_data_len; idex++)
    {
        tar_list->data_ptr[idex]=0;
    }
    tar_list->data_len = 0;
    return 1;
}

/*!
 @brief Get the list len object
 
 @param tar_obj 
 @return uint16_t 
 */
uint16_t get_list_len(mem_control *tar_obj)
{
    return tar_obj->data_len;
}


/*!
 @brief list_isFull
        full : 1
    not full : 0
 @param tar_obj 
 @return uint8_t 
 */
uint8_t list_isFull(mem_control *tar_obj)
{
    uint16_t remain_len ;
    remain_len = tar_obj->__max_data_len - (tar_obj->data_len) ;
    if(remain_len)
    { 
        return 0; // not full
    } 
    return 1 ; // full
}

/*!
 @brief  返回一组数据中第一个峰值的 下标，和有效性
 @param _src 
 @param src_len 
 @return Peak_OrNo 
 */
Peak_OrNo find_max( uint32_t *_src , const uint16_t src_len)
{
  uint16_t seach_idx = 0;
  Peak_OrNo __peak = {.peak_loc=0,.peak_valid = 0};
  uint32_t _peak_val = 0;
    for(seach_idx = 0 ; seach_idx < src_len ;  seach_idx++)
    {
        if( _src[seach_idx] >= _peak_val )
        {
            _peak_val =  _src[seach_idx];
           __peak.peak_loc  = seach_idx;        
        }
        else
        {
            break;
        }
    }
    if( __peak.peak_loc == 0 ||  __peak.peak_loc == (src_len-1))
    {
        __peak.peak_valid = 0;
        __peak.peak_loc = 0;
        return __peak;
    }
    __peak.peak_valid = 1;
    return __peak;
}
/*!
 @brief 返回平均值
 
 @param src_list 
 @param len 
 @return double 
 */
int32_t mean_fun(int32_t *src_list,const uint16_t len)
{
    uint16_t idx = 0;
    int32_t sum_x=0;
    int32_t rest=0.0;
    for (idx=0;idx<len;idx++)
    {
        sum_x += src_list[idx];
    }
    rest = (int32_t)((double)(sum_x)/(double)len);
    return (int32_t)rest;
}
/*!
 @brief 反转该窗口内的信号
 
 @param _ser_src 
 @param _src_len 
 */
void Reverse_pos_neg(uint32_t *_ser_src , const uint16_t _src_len)
{

   uint32_t base_up = 0;
   //this window ,biggest val
   uint8_t ix=0;
   //  距离 0 线的距离    xxx = 100
   for(ix=0 ; ix<_src_len ; ix++)
   {
      if ( _ser_src[ix] > base_up )
      {
        base_up = _ser_src[ix];
      }
   }
   for (ix =0;ix<_src_len;ix++)
   {
      _ser_src[ix] = 100+ _ser_src[ix];
   }
}



/******* file end ** **************/

